package domain

import (
	"usbipproxy/common"
	"usbipproxy/pb"
)

/*
type UserStatus int

const (
	StatusUserNone     UserStatus = 0
	StatusUserLoginIng UserStatus = 1
	StatusUserKicked   UserStatus = 3
	StatusUserTimeOut  UserStatus = 4
)
*/

type DispatchClient struct {
	usbIpType pb.UsbIpType
	did       string
	sessRepo  TcpConnSessRepo
}

func (u *DispatchClient) IsCorrectReqId(reqId string) bool {
	if u == nil {
		return false
	}
	return reqId == "" || reqId == u.did
}

func (u *DispatchClient) GetSessRepo() TcpConnSessRepo {
	return u.sessRepo
}

func (u *DispatchClient) UpdateBaseParamForOut(msg *pb.ProxyMsg) {
	if msg == nil {
		return
	}
	uuidStr := common.GetPureUuid()
	if msg.GetBase() == nil {
		msg.Base = &pb.BaseParam{
			FromId: u.GetConnId(),
			ToId:   0,
			ReqId:  uuidStr,
		}
	} else {
		if msg.Base.ReqId == "" {
			msg.Base.ReqId = uuidStr
		}
		msg.Base.ToId = msg.GetBase().GetFromId()
		msg.Base.FromId = u.GetConnId()
	}
}

func (u *DispatchClient) UpdateBaseParamWithToId(msg *pb.ProxyMsg, toId int64) {
	if msg == nil {
		return
	}
	uuidStr := common.GetPureUuid()
	if msg.GetBase() == nil {
		msg.Base = &pb.BaseParam{
			FromId: 0,
			ToId:   toId,
			ReqId:  uuidStr,
		}
	} else {
		if msg.Base.ReqId == "" {
			msg.Base.ReqId = uuidStr
		}
		msg.Base.ToId = toId
	}
}

func (u *DispatchClient) SetDid(did string) {
	if u == nil {
		return
	}

	u.did = did
}

func (u *DispatchClient) GetDid() string {
	if u == nil {
		return ""
	}

	return u.did
}

func (u *DispatchClient) GetConnId() int64 {
	if u == nil {
		return 0
	}

	return u.sessRepo.GetConnId()
}

func (u *DispatchClient) GetusbIpType() pb.UsbIpType {
	if u == nil {
		return pb.UsbIpType_USBIP_TYPE_NONE
	}

	return u.usbIpType
}

func (u *DispatchClient) GetToType() pb.UsbIpType {
	if u == nil {
		return pb.UsbIpType_USBIP_TYPE_NONE
	}

	if u.usbIpType == pb.UsbIpType_USBIP_TYPE_SERVER {
		return pb.UsbIpType_USBIP_TYPE_CLIENT
	}

	return pb.UsbIpType_USBIP_TYPE_SERVER
}

func (u *DispatchClient) SetUsbIpType(usbIpType pb.UsbIpType) {
	if u == nil {
		return
	}

	u.usbIpType = usbIpType
}

func NewDispatchClient(did string, usbIpType pb.UsbIpType, sessRepo TcpConnSessRepo) *DispatchClient {
	return &DispatchClient{
		usbIpType: usbIpType,
		did:       did,
		sessRepo:  sessRepo,
	}
}
